package com.easylinkin.linkappapi.progress.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.progress.entity.ProgressLagInfo;
import com.easylinkin.linkappapi.progress.entity.ProgressWarnConfig;
import com.easylinkin.linkappapi.progress.entity.vo.CalProgressWarnVo;
import com.easylinkin.linkappapi.progress.entity.vo.ProgressWarnVo;
import com.easylinkin.linkappapi.progress.service.ProgressWarnConfigService;
import com.easylinkin.linkappapi.progress.service.ProgressWarnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 进度管理/进度预警
 *
 * @author zhengwen
 * @date 2023/11/22
 */
@Slf4j
@RestController
@RequestMapping("/progressWarn")
public class ProgressWarnController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ProgressWarnService progressWarnService;

    @Resource
    private ProgressWarnConfigService progressWarnConfigService;


    /**
     * 获取进度预警配置
     *
     * @return 统一出参
     */
    @GetMapping("/getProgressWarnConfig")
    public RestMessage getProgressWarnConfig() {
        return progressWarnConfigService.getProgressWarnConfig();
    }

    /**
     * 保存或更新进度预警配置
     *
     * @param progressWarnConfig 进度预警配置
     * @return 统一出参
     */
    @PostMapping("/saveOrUpdateProgressWarnConfig")
    public RestMessage saveOrUpdateProgressWarnConfig(@RequestBody ProgressWarnConfig progressWarnConfig) {
        Assert.notNull(progressWarnConfig, "进度预警配置不能为空");
        return progressWarnConfigService.saveOrUpdateProgressWarnConfig(progressWarnConfig);
    }


    /**
     * 分页查询任务预警数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("/selectDtoPage")
    public RestMessage selectDtoPage(@RequestBody RequestModel<ProgressWarnVo> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        return progressWarnService.selectDtoPage(requestModel.getPage(), requestModel.getCustomQueryParams());
    }

    /**
     * 获取任务节点滞后信息
     *
     * @param progressTaskId 进度任务id
     * @return 统一出参
     */
    @GetMapping("/getLagInfo/{progressTaskId}")
    public RestMessage getLagInfo(@PathVariable Long progressTaskId) {
        Assert.notNull(progressTaskId, "进度任务id不能为空");
        return progressWarnService.getLagInfo(progressTaskId);
    }

    /**
     * 保存或更新滞后信息
     *
     * @param progressLagInfo 进度滞后原因
     * @return 新增结果
     */
    @PostMapping("/saveOrUpdateLagInfo")
    public RestMessage saveOrUpdateLagInfo(@RequestBody ProgressLagInfo progressLagInfo) {
        Assert.notNull(progressLagInfo, "滞后信息不能为空");
        Assert.notNull(progressLagInfo.getProgressId(), "进度任务id不能为空");
        return progressWarnService.saveOrUpdateLagInfo(progressLagInfo);
    }

    /**
     * 导出
     *
     * @param requestModel 查询分页对象
     * @param request      请求
     * @param response     响应
     */
    @PostMapping("/export")
    public void export(@RequestBody RequestModel<ProgressWarnVo> requestModel, HttpServletRequest request, HttpServletResponse response) {

        try {
            progressWarnService.exportAll(requestModel, request, response);
        } catch (IOException e) {
            throw new RuntimeException("excel导出失败！IOException异常" + e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException("excel导出失败！" + e.getMessage());
        }
    }

    /**
     * 删除预警配置
     *
     * @param idList 主键列表
     * @return 删除结果
     */
    @DeleteMapping
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        Assert.notEmpty(idList, "未选择删除数据");
        return RestBuilders.successBuilder().success(progressWarnConfigService.deleteByIds(idList)).build();
    }

    /**
     * 手动计算进度预警
     *
     * @param calProgressWarnVo 计算参数
     * @return 计算结果
     */
    @PostMapping("/manualCalProgressWarn")
    public RestMessage manualCalProgressWarn(@RequestBody CalProgressWarnVo calProgressWarnVo) {
        //Assert.notNull(calProgressWarnVo, "计算参数不能为空");
        //Assert.notNull(calProgressWarnVo.getType(), "计算类型不能为空");
        //Assert.notNull(calProgressWarnVo.getDataType(), "数据类型不能为空");
        return progressWarnService.manualCalProgressWarn(calProgressWarnVo);
    }

    /**
     * 测试自动任务计算进度预警
     * @return 触发结果
     */
    @PostMapping("/testJobCalProgressWarn")
    public RestMessage testJobCalProgressWarn() {
        progressWarnService.progressWarnCalJob();
        return RestBuilders.successBuilder().build();
    }

}

